package network.udp.udp_group;

import java.net.DatagramPacket;
import java.net.InetAddress;
import java.net.MulticastSocket;

/**
 * @program: baseModule
 * @description:
 * @author: zhanghz001
 * @create: 2020-10-26 16:13
 **/
public class GroupReceive {
    public static void main(String[] args) {
        receive();

    }

    private static void receive() {
        MulticastSocket ms = null;
        try {
            //创建socket
            ms = new MulticastSocket(10000);
            //接收数据包准备
            byte[] bytes = new byte[1024];
            DatagramPacket receivePackage = new DatagramPacket(bytes, bytes.length);
            //添加到组中去
            ms.joinGroup(InetAddress.getByName("224.0.0.1"));

            //接收数据包
            ms.receive(receivePackage);

            byte[] data = receivePackage.getData();
            int length = receivePackage.getLength();
            System.out.println(new String(data, 0, length));

            //释放资源
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (ms != null) {
                ms.close();
            }
        }
    }
}
